package com.dd.cloud.user.service.patient.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.ConsultConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.entity.consult.ConsultSubscribe;
import com.dd.cloud.user.entity.patient.PatientUserAppraise;
import com.dd.cloud.user.entity.patient.PatientUserComplaint;
import com.dd.cloud.user.mapper.patient.PatientUserComplaintMapper;
import com.dd.cloud.user.req.patient.PatientUserAppraiseReq;
import com.dd.cloud.user.req.patient.PatientUserComplaintReq;
import com.dd.cloud.user.res.patient.PatientUserAppraiseRes;
import com.dd.cloud.user.res.patient.PatientUserComplaintRes;
import com.dd.cloud.user.service.consule.IConsultSubscribeService;
import com.dd.cloud.user.service.patient.IPatientUserComplaintService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户投诉表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-07-17
 */
@Service
public class PatientUserComplaintServiceImpl extends ServiceImpl<PatientUserComplaintMapper, PatientUserComplaint> implements IPatientUserComplaintService {

    @Autowired
    private IConsultSubscribeService consultSubscribeService;

    @Override
    public void addPatientUserComplaint(PatientUserComplaintReq patientUserComplaintReq) {
        LambdaQueryWrapper<PatientUserComplaint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PatientUserComplaint::getConsultId, patientUserComplaintReq.getConsultId());
        Integer count = getBaseMapper().selectCount(wrapper);
        if (count > 0) {
            throw new ZekeException("该问诊订单已投诉,请勿重复投诉");
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.PATIENT).get();
        Integer userId = loginInfo.getId();
        PatientUserComplaint patientUserComplaint = new PatientUserComplaint();
        BeanUtils.copyProperties(patientUserComplaintReq, patientUserComplaint);
        patientUserComplaint.setUserId(userId);
        boolean flag = this.save(patientUserComplaint);
        if (!flag) {
            throw new ZekeException("保存失败");
        }
        //更新订单状态
        LambdaUpdateWrapper<ConsultSubscribe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ConsultSubscribe::getStatus, ConsultConstants.Status.POOREVAL)
                .eq(ConsultSubscribe::getId,patientUserComplaintReq.getConsultId());
        consultSubscribeService.update(updateWrapper);
    }

    @Override
    public IPage<PatientUserComplaintRes> queryPatientUserComplaintPage(PageReq pageReq) {
        IPage<PatientUserComplaintRes> page = pageReq.pagePojo();
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.PATIENT).get();
        IPage<PatientUserComplaintRes> res = this.getBaseMapper().queryPatientUserComplaintPage(page, loginInfo.getId());
        return res;
    }
}
